package com.nike.amoeba.adapter

import android.view.View
import android.widget.AdapterView
import android.widget.Filter
import android.widget.Filterable
import androidx.appcompat.widget.AppCompatCheckBox
import androidx.appcompat.widget.AppCompatSpinner
import com.blankj.utilcode.util.LogUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.nike.amoeba.R
import com.nike.amoeba.bean.DataBean
import com.nike.amoeba.ext.setAdapterAnimation
import com.nike.amoeba.util.SettingUtil

/**
 * @Author nike
 * @Date 2023/7/7 17:05
 * @Description
 */
class StockRuAdapter(data: ArrayList<DataBean>) :
    BaseQuickAdapter<DataBean, BaseViewHolder>(
        R.layout.i_stock_ru, data
    ), Filterable {

    init {
        setAdapterAnimation(SettingUtil.getListMode())
    }

    override fun convert(viewHolder: BaseViewHolder, bean: DataBean) {
        mFilterList[viewHolder.layoutPosition].run {
            viewHolder.setText(R.id.tv_title, EPC)
            viewHolder.setText(R.id.tv_text, context.getString(R.string.level) + "：$Level")
            viewHolder.setText(R.id.tv_text3, context.getString(R.string.numberr) + "：$NoOfAll")
            viewHolder.setText(R.id.tv_text1, context.getString(R.string.unit) + "：$MUnitName")
            viewHolder.setText(
                R.id.tv_text2,
                context.getString(R.string.storage_location) + "："
            )
            val cb = viewHolder.getView<AppCompatCheckBox>(R.id.cb_)
            cb.isChecked = if (Type == 1)true else false

            viewHolder.getView<AppCompatSpinner>(R.id.spinner).run {
                if (!StorageUnitList.isNullOrEmpty()){
                    visibility = View.VISIBLE
                    adapter = StockStorageAdapter(context, StorageUnitList!!)
                    onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
                        override fun onItemSelected(
                            parent: AdapterView<*>?,
                            view: View?,
                            position: Int,
                            id: Long
                        ) {
                            val bean = StorageUnitList!![position]
                            SRoNo = bean.SRoNo
                            SUnitName = bean.SUnitName
                        }

                        override fun onNothingSelected(parent: AdapterView<*>?) {
                            // 当没有选择项时触发的逻辑
                        }
                    }
                    StorageUnitList!!.forEachIndexed { index, c ->
                        if (c.SRoNo == SRoNo){
                            setSelection(index)
                            return@forEachIndexed
                        }
                    }
                }
            }
        }
    }

    var mFilterList = ArrayList<DataBean>()

    fun appendList(list: List<DataBean>) {
        data = list as MutableList<DataBean>
        //这里需要初始化filterList
        mFilterList = list as ArrayList<DataBean>
    }

    override fun getFilter(): Filter {
        return object : Filter() {
            //执行过滤操作
            override fun performFiltering(charSequence: CharSequence): FilterResults {
                val charString = charSequence.toString()
                if (charString.isEmpty()) {
                    //没有过滤的内容，则使用源数据
                    mFilterList = data as ArrayList<DataBean>
                } else {
                    val filteredList: MutableList<DataBean> = ArrayList()
                    for (i in data.indices) {
                        val bean = data[i]
                        if (bean.EPC?.contains(charString, ignoreCase = true) == true
//                            || bean.SUnitName?.contains(charString, ignoreCase = true) == true
                        ) {
                            filteredList.add(bean)
                        }
                    }
                    mFilterList = filteredList as ArrayList<DataBean>
                }
                val filterResults = FilterResults()
                filterResults.values = mFilterList
                return filterResults
            }

            //把过滤后的值返回出来
            override fun publishResults(charSequence: CharSequence, filterResults: FilterResults) {
                mFilterList = filterResults.values as ArrayList<DataBean>
                notifyDataSetChanged()
            }
        }
    }


    override fun getItemCount(): Int {
        return mFilterList.size
    }

    override fun hashCode(): Int {
        return mFilterList.hashCode()
    }

}